package main

import "fmt"

func main(){
	fmt.Println(towSumForce([]int{10,20,30,40,50},90))
	fmt.Println(towSumElegrant([]int{10,20,30,40,50},90))
}

/*
	暴力解法：
		两层循环，外部循环的值与target的差值为内部循环需要定位的值
*/
func towSumForce(nums []int, target int)(result []int){
	length := len(nums)
	if length < 1{
		return
	}
	for i := 0; i < length ; i++ {
		for j := i; j< length ;  j++{
			if nums[j] + nums[i] == target{
				return []int{i,j}
			}
		}
	}
	return
}
/*
	优雅解法：
		一层循环，引入map，循环的当前值与target的差值在map中查找，如果不存在则将值作为键放入map，值的索引作为map的值
*/
func towSumElegrant(nums []int, target int)(result []int){
	length := len(nums)
	if length < 1{
		return
	}
	var m = make(map[int]int)
	for key, value := range nums {
		v , ok := m[target - value]
		if !ok{
			m[value] = key
		}else{
			return []int{v,key}
		}
	}
	return
}